package com.sdd.rpi_mobile.infoTypes;

import com.sdd.rpi_mobile.R;
import com.sdd.rpi_mobile.homeScreen.GridChooser;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.text.util.Linkify;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

/** Displays and controls the information for the Places of Interest feature. */
public class PlacesInfoActivity extends Activity {
	
	private TextView mTitle, mDescription, mLink, mPhone, mHours;

	@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.places_info_template);
		// Enable the Home button in the action bar.
		if (Build.VERSION.SDK_INT >= 14) {
			getActionBar().setHomeButtonEnabled(true);
		}
				
		Bundle bundle = this.getIntent().getExtras();
		int placeId = bundle.getInt("resource");
		
		//Get the resource of the selected item e.g. Dinosaur BBQ.
		Resources res = getResources();
		res.getStringArray(placeId);
		String[] place_info = res.getStringArray(placeId);
		PlacesInfo info = new PlacesInfo(place_info);
		
		//Setup the title.
		mTitle = (TextView) findViewById(R.id.header);
		mTitle.setText(info.getName());
		
		//Setup the description.
		mDescription = (TextView) findViewById(R.id.place_description);
		mDescription.setText(info.getDescription());
		
		//Setup the phone number.
		mPhone = (TextView) findViewById(R.id.place_phone);
		mPhone.setText(info.getPhone());
		
		//Setup the link.
		mLink = (TextView) findViewById(R.id.place_link);
		mLink.setText(info.getLink());
		
		//Setup the opening hours.
		mHours = (TextView) findViewById(R.id.place_hours);
		mHours.setText(info.getHours());
		Linkify.addLinks(mLink, Linkify.ALL);
		Linkify.addLinks(this.mPhone, Linkify.ALL);
		Linkify.addLinks(this.mDescription, Linkify.ALL);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.places_info_template, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    switch (item.getItemId()) {
	        case android.R.id.home:
	            // app icon in action bar clicked; go home
	            Intent intent = new Intent(this, GridChooser.class);
	            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
	            startActivity(intent);
	            return true;
	        default:
	            return super.onOptionsItemSelected(item);
	    }
	}
}
